package cn.edu.zafu.bigdata.x.huangxihao202211080211.TCP1;

import java.io.*;
import java.net.*;

public class Client11 {
    public static void main(String[] args) {
        // 服务端的IP地址和端口号
        String serverAddress = "127.0.0.1"; // 可以修改为服务端的实际IP
        int port = 12345;

        try (Socket socket = new Socket(serverAddress, port);
             BufferedReader userInput = new BufferedReader(new InputStreamReader(System.in));
             BufferedReader serverInput = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             PrintWriter serverOutput = new PrintWriter(socket.getOutputStream(), true)) {

            System.out.println("已连接到服务器，输入消息发送。输入 'exit' 退出。");

            String message;
            while (true) {
                // 从用户输入获取消息
                System.out.print("请输入消息: ");
                message = userInput.readLine();

                if ("exit".equalsIgnoreCase(message)) {
                    break;
                }

                // 发送消息到服务器
                serverOutput.println(message);

                // 接收并显示服务器的回复
                String response = serverInput.readLine();
                System.out.println(response);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

